package RRHH_Modelo;
import Conexion.Conexion;
import com.sun.xml.security.core.ai.IdentityType;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class RHH_CARGO_DEP_REG_SUC{
    private double ID_REGIONAL;
    private double ID_DEPTO;
    private double ID_SUCURSAL;
    private int ID_CARGO;
    private double ID;
    private Conexion con = null;
    
    public RHH_CARGO_DEP_REG_SUC(Conexion con){
        this.con=con;
    }
    
    public RHH_CARGO_DEP_REG_SUC(double ID,int ID_CARGO,double ID_SUCURSAL,double ID_DEPTO,double ID_REGIONAL){
        
        this.ID =ID;
        this.ID_CARGO =ID_CARGO;
        this.ID_SUCURSAL =ID_SUCURSAL==1?0:ID_SUCURSAL;
        this.ID_DEPTO =ID_DEPTO==1?0:ID_DEPTO;
        this.ID_REGIONAL =ID_REGIONAL==1?0:ID_REGIONAL;
    }
    
    public boolean Insertar()throws Exception
    {
        
            String consulta = "";
            if(ID_SUCURSAL ==0 && ID_REGIONAL ==0)
            consulta +="insert into BROKER.RHH_CARGO_DEP_REG_SUC(ID,ID_CARGO,ID_SUCURSAL,ID_DEPTO,ID_REGIONAL) VALUES ("+ID+","+ID_CARGO+",null,"+ID_DEPTO+",null)";
            if(ID_SUCURSAL ==0 && ID_REGIONAL !=0)
            consulta +="insert into BROKER.RHH_CARGO_DEP_REG_SUC(ID,ID_CARGO,ID_SUCURSAL,ID_DEPTO,ID_REGIONAL) VALUES ("+ID+","+ID_CARGO+",null,"+ID_DEPTO+","+ID_REGIONAL+")";
            if(ID_SUCURSAL !=0 && ID_REGIONAL !=0)
            consulta +="insert into BROKER.RHH_CARGO_DEP_REG_SUC(ID,ID_CARGO,ID_SUCURSAL,ID_DEPTO,ID_REGIONAL) VALUES ("+ID+","+ID_CARGO+","+ID_SUCURSAL+","+ID_DEPTO+","+ID_REGIONAL+")";
            con.EjecutarSentencia(consulta);
            return true;
    }
    public boolean modificar()throws Exception
    {
            String suc="null ";
            if(ID_SUCURSAL>0.0)suc=ID_SUCURSAL+"";
            String reg="null ";
            if(ID_REGIONAL>0.0)reg=ID_REGIONAL+"";
            String dep="null ";
            if(ID_DEPTO>0.0)dep=ID_DEPTO+"";
            String consulta = "";
            consulta +="update BROKER.RHH_CARGO_DEP_REG_SUC set ID_SUCURSAL="+suc+",ID_DEPTO="+dep+",ID_REGIONAL="+reg+" where ID_CARGO="+ID_CARGO;
            
            con.EjecutarSentencia(consulta);
            return true;
    }
    
    public boolean EliminarXid(){
        try{
            String consulta = "delete from BROKER.RHH_CARGO_DEP_REG_SUC where ID = "+getID();
            con.EjecutarSentencia(consulta);
            return true;
        }
        catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }
    public List<RHH_CARGO_DEP_REG_SUC> CargoAsignado(int tipo, String id_depto, String id_regional, String id_sucursal){
        try
        {
            String consulta = "";
            if(tipo==3)
                consulta = "select * from BROKER.RHH_CARGO_DEP_REG_SUC where ID_DEPTO = "+id_depto+" and  ID_REGIONAL = "+id_regional+" and ID_SUCURSAL= "+id_sucursal;
            if(tipo==2)
                consulta = "select * from BROKER.RHH_CARGO_DEP_REG_SUC where ID_DEPTO = "+id_depto+" and  ID_REGIONAL = "+id_regional+" and ID_SUCURSAL is null";
            if(tipo==1)
                consulta = "select * from BROKER.RHH_CARGO_DEP_REG_SUC where ID_DEPTO = "+id_depto+" and  ID_REGIONAL is null and ID_SUCURSAL is null";
            ResultSet rs = con.EjecutarConsulta(consulta);
            return Cargar(rs);
        }
        catch(Exception e){
            e.printStackTrace();}
        return null;
    }
   
    
    public RHH_CARGO_DEP_REG_SUC BuscarId(double id_cargo, double id_depto, double id_regional, double id_sucursal){
        try
        {
            String consulta = "";
            if(id_regional ==0 && id_sucursal ==0)
                consulta = "select * from BROKER.RHH_CARGO_DEP_REG_SUC where ID_DEPTO = "+id_depto+" and  ID_REGIONAL = null and ID_SUCURSAL= null and ID_CARGO="+id_cargo;
            if(id_regional !=0 && id_sucursal ==0)
                consulta = "select * from BROKER.RHH_CARGO_DEP_REG_SUC where ID_DEPTO = "+id_depto+" and  ID_REGIONAL = "+id_regional+" and ID_SUCURSAL= null and ID_CARGO="+id_cargo;
            if(id_regional !=0 && id_sucursal !=0)
                consulta = "select * from BROKER.RHH_CARGO_DEP_REG_SUC where ID_DEPTO = "+id_depto+" and  ID_REGIONAL = "+id_regional+" and ID_SUCURSAL="+id_sucursal+" null and ID_CARGO="+id_cargo;
            
            ResultSet rs = con.EjecutarConsulta(consulta);
            return Cargar(rs).get(0);
        }
        catch(Exception e){
            e.printStackTrace();}
        return null;
    }
    
    public List<RHH_CARGO_DEP_REG_SUC> BuscarCargos(double id_depto, double id_regional, double id_sucursal){
        try
        {
            String consulta = "";
            if(id_regional ==0 && id_sucursal ==0)
                consulta = "select * fromKER.RHH_CARGO_DEP_REG_SUC where ID_DEPTO = "+id_depto+" and  ID_REGIONAL = null and ID_SUCURSAL= null";
            if(id_regional !=0 && id_sucursal ==0)
                consulta = "select * from BROKER.RHH_CARGO_DEP_REG_SUC where ID_DEPTO = "+id_depto+" and  ID_REGIONAL = "+id_regional+" and ID_SUCURSAL= null";
            if(id_regional !=0 && id_sucursal !=0)
                consulta = "select * from BROKER.RHH_CARGO_DEP_REG_SUC where ID_DEPTO = "+id_depto+" and  ID_REGIONAL = "+id_regional+" and ID_SUCURSAL="+id_sucursal;
            
            ResultSet rs = con.EjecutarConsulta(consulta);
            return Cargar(rs);
        }
        catch(Exception e){
            e.printStackTrace();}
        return null;
    }
    
    public RHH_CARGO_DEP_REG_SUC BuscarXCargos(int idCargo) throws SQLException{
            String consulta = "select * from  BROKER.RHH_CARGO_DEP_REG_SUC where ID_CARGO="+idCargo;
            ResultSet rs = con.EjecutarConsulta(consulta);
            List<RHH_CARGO_DEP_REG_SUC> lista= Cargar(rs);
            if(lista.size()>0)
                return lista.get(0);
            else
            return null;
    }
    
    public List<RHH_CARGO_DEP_REG_SUC> Todos(){
        try{String consulta = "select * from BROKER.RHH_CARGO_DEP_REG_SUC";
        ResultSet rs = con.EjecutarConsulta(consulta);
        return Cargar(rs);}
        catch(Exception e){
            e.printStackTrace();}
        return null;
    }    

    
    public List<RHH_CARGO_DEP_REG_SUC> Cargar(ResultSet rs){
        try{
            List<RHH_CARGO_DEP_REG_SUC> lista = new ArrayList<RHH_CARGO_DEP_REG_SUC>();
            while(rs.next()){
                RHH_CARGO_DEP_REG_SUC objeto = new RHH_CARGO_DEP_REG_SUC(con);
                
                objeto.setID_REGIONAL(rs.getDouble("ID_REGIONAL"));
                objeto.setID_DEPTO(rs.getDouble("ID_DEPTO"));
                objeto.setID_SUCURSAL(rs.getDouble("ID_SUCURSAL"));
                objeto.setID_CARGO(rs.getInt("ID_CARGO"));
                objeto.setID(rs.getDouble("ID"));
                objeto.setCon(con);
                lista.add(objeto);
            }
            return lista;
        }
        catch(SQLException ex){
            ex.printStackTrace();
            return null;}
    }
    
    public double getID_REGIONAL(){
        return ID_REGIONAL;
    }
    public void setID_REGIONAL(double ID_REGIONAL){
        this.ID_REGIONAL=ID_REGIONAL;
    }
    
    public double getID_DEPTO(){
        return ID_DEPTO;
    }
    public void setID_DEPTO(double ID_DEPTO){
        this.ID_DEPTO=ID_DEPTO;
    }
    
    public double getID_SUCURSAL(){
        return ID_SUCURSAL;
    }
    public void setID_SUCURSAL(double ID_SUCURSAL){
        this.ID_SUCURSAL=ID_SUCURSAL;
    }
    
    public int getID_CARGO(){
        return ID_CARGO;
    }
    public void setID_CARGO(int ID_CARGO){
        this.ID_CARGO=ID_CARGO;
    }
    
    public double getID(){
        return ID;
    }
    public void setID(double ID){
        this.ID=ID;
    }
    
    public Conexion getCon() {
        return con;
    }
    public void setCon(Conexion con) {
        this.con = con;
    }
    
}
